package ex403.point;

import java.util.HashSet;

public class Point2DSet {
    private HashSet<Point2D> set;
    private Point2DPair nearest;
    private Point2DPair farthest;

    public Point2DSet() {
        set = new HashSet<>();
    }

    public void addPoint2D(Point2D p) {
        set.add(p);
    }

    @Override
    public String toString() {
        return set.toString();
    }

    public int getNumberOfPoint2D() {
        return set.size();
    }

    /**
     * 更新最近和最远点对
     */
    public void update() {
        Point2D[] points = set.toArray(new Point2D[0]);
        nearest = new Point2DPair(points[0], points[1]);
        farthest = new Point2DPair(points[0], points[1]);
        for (int i = 0; i < points.length - 1; i++) {
            for (int j = i + 1; j < points.length; j++) {
                if (points[i].distanceTo(points[j]) < nearest.getDistance()) {
                    nearest.setP1(points[i]);
                    nearest.setP2(points[j]);
                }
                if (points[i].distanceTo(points[j]) > farthest.getDistance()) {
                    farthest.setP1(points[i]);
                    farthest.setP2(points[j]);
                }
            }
        }
    }

    public Point2DPair getNearestPair() {
        return nearest;
    }

    public Point2DPair getFarthestPair() {
        return farthest;
    }
}
